#include "work_window.h"
#include<QLabel>
#include<QPushButton>
#include<QOverload>
#include<QMenuBar>
#include<QApplication>
Work_Window::Work_Window(QWidget *parent)
    : QMainWindow{parent}
{
    this->setMinimumHeight(800);
    this->setMinimumWidth(800);
    stackedWidget = new QStackedWidget(this);
    setCentralWidget(stackedWidget);

    // data_updater = new QTimer();
    // connect(data_updater, SIGNAL(timeout()),this,SLOT(update_device_value()));
    // data_updater->start(1000);
    //0 能耗概括面板
    EMS_Summary *summary_panel=new EMS_Summary;
    stackedWidget->addWidget(summary_panel);
    //1 能耗监控
    monitor=new DeviceMonitor(this);
    stackedWidget->addWidget(monitor);
    //2 历史查询
    history_page = new HistoryPage();
    stackedWidget->addWidget(history_page);
    // 数据分析
    EnergyDashboard *anasis=new EnergyDashboard;
    stackedWidget->addWidget(anasis);
    //3 系统配置
    system_settings_page=new SystemSettingsPage(nullptr);
    stackedWidget->addWidget(system_settings_page);
    //4
    //创建串口工具页面
    serial_tool_widget=new SerialToolWidget(monitor,history_page);
    stackedWidget->addWidget(serial_tool_widget);

    //为各变量赋值
    summary_panel->device_monitor=monitor;
    summary_panel->serial_tool_widget=serial_tool_widget;
    summary_panel->setupUI();
    system_settings_page->summary=summary_panel;

    anasis->serial_tool_widget=serial_tool_widget;
    // 创建工具栏和按钮
    toolBar = new QToolBar("Full Width Toolbar");
    toolBar->setMovable(false); // 防止工具栏被移动
    this->addToolBar(Qt::TopToolBarArea, toolBar); // 添加到顶部区域
    toolBar->setFloatable(false); // 防止工具栏浮动（可选）

    // 创建按钮容器
    QWidget *btnContainer = new QWidget;
    QHBoxLayout *btnLayout = new QHBoxLayout(btnContainer);
    btnLayout->setContentsMargins(0, 0, 0, 0); // 移除边距
    btnLayout->setSpacing(0);                  // 移除按钮间距

    buttonGroup = new QButtonGroup(this);

    QStringList str_list;
    str_list<<"能耗总概括";
    str_list<<"能耗监控";
    str_list<<"历史查询";
    str_list<<"数据分析";
    str_list<<"系统配置";
    str_list<<"串口测试工具";

    // 创建切换按钮
    for (int i = 0; i < str_list.count(); ++i) {
        QPushButton *btn = new QPushButton(QString("%1").arg(str_list.at(i)));
        btn->setCheckable(true);
        btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); // 水平扩展
        btn->setMinimumWidth(80); // 设置最小宽度

        // 添加样式
        btn->setStyleSheet(
            "QPushButton {"
            "   border: 1px solid #3498db;"
            "   padding: 8px;"
            "   background-color: #f0f0f0;"
            "}"
            "QPushButton:checked {"
            "   background-color: #3498db;"
            "   color: white;"
            "}"
            "QPushButton:hover {"
            "   background-color: #2980b9;"
            "   color: white;"
            "}"
            );

        btnLayout->addWidget(btn);
        buttonGroup->addButton(btn, i);
    }
    // 将按钮容器添加到工具栏
    toolBar->addWidget(btnContainer);

    // qDebug()<<login_status.second<<"test";

    // 连接信号槽
    connect(buttonGroup, &QButtonGroup::idClicked,
            stackedWidget, &QStackedWidget::setCurrentIndex);
    // 设置默认选中第一个按钮
    buttonGroup->button(0)->setChecked(true);
}


Work_Window::~Work_Window()
{

}

void Work_Window::createPage(int index, const QString &name, const QColor &color)
{
    QWidget *page = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout(page);
    layout = new QVBoxLayout(page);
    layout->addWidget(new QLabel(name, page));

    QWidget *colorWidget = new QWidget(page);
    colorWidget->setStyleSheet(QString("background-color: %1;").arg(color.name()));
    layout->addWidget(colorWidget);

    stackedWidget->addWidget(page);
}

void Work_Window::closeEvent(QCloseEvent *event) {
    event->accept(); // 接受关闭事件
    // 检查是否所有顶层窗口都已关闭
    if (QApplication::topLevelWindows().isEmpty()) {
        QApplication::quit(); // 手动退出应用
    }

    QApplication::quit(); // 手动退出应用
}

//更新权限
void Work_Window::update_authority(QString& privileges)
{
    if(privileges=="管理员")
    {
        for(int i=2;i<4;i++)
        {
            buttonGroup->button(i)->setEnabled(true);
        }
    }
    else
    {
        for(int i=2;i<4;i++)
        {
            buttonGroup->button(i)->setEnabled(false);
        }
    }
}

